/**
 * jims.
 */
package com.jims.master.common.utils;

import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.sys.dao.HospitalDictDao;
import com.jims.master.modules.sys.entity.HospitalDict;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * web server 工具类
 *
 * @author ThinkGem
 * @version 2014-6-29
 */
public class WebServiceUtils {

	private static HospitalDictDao hospitalDictDao = SpringContextHolder.getBean(HospitalDictDao.class);

	//调用接口方法（itemcode表示map的value值，keyName表示map中key的名称）
	public static List<BaseDto> getExamList(String orgId , String url, List<BaseDto> list) {
		List<BaseDto> resultList=new ArrayList<BaseDto>();
		HashMap<String, Object> params= new HashMap<String, Object>();
		for(BaseDto dto:list){
			params.putAll(dto);
		}
		HospitalDict hospitalDict = new HospitalDict();
		hospitalDict.setOrgId(orgId);
		HospitalDict hospitalDictClinicMaster = hospitalDictDao.getHospitalDictsByEntity(hospitalDict);
		String serverIp = "http://";
		if(hospitalDictClinicMaster!=null)
			serverIp += hospitalDictClinicMaster.getInputCode();
		RestUtils<BaseDto,HashMap> restUtils =new RestUtils<BaseDto ,HashMap>(serverIp);
		try {
			Map<String, Object> map = restUtils.findByList(url, BaseDto.class, params, params);
			resultList= (List<BaseDto>) map.get("body");
		}catch (Exception e){
			e.getMessage();
		}
		return resultList;
	}

	public static BaseDto getExamEngit(String orgId , String url, List<BaseDto> list) {
		List<BaseDto> resultList=new ArrayList<BaseDto>();
		BaseDto baseDto1 = new BaseDto();
		HashMap<String, Object> params= new HashMap<String, Object>();
		for(BaseDto dto:list){
			params.putAll(dto);
		}
		HospitalDict hospitalDict = new HospitalDict();
		hospitalDict.setOrgId(orgId);
		HospitalDict hospitalDictClinicMaster = hospitalDictDao.getHospitalDictsByEntity(hospitalDict);
		String serverIp = "http://";
		if(hospitalDictClinicMaster!=null)
			serverIp += hospitalDictClinicMaster.getValue();
		RestUtils<BaseDto,HashMap> restUtils =new RestUtils<BaseDto ,HashMap>(serverIp);
		try {
			Map<String, Object> map = restUtils.findById(url, BaseDto.class, params, params,"");
			baseDto1= (BaseDto) map.get("body");
		}catch (Exception e){
			e.getMessage();
		}
		return baseDto1;
	}

	//接口保存方法
	public static String saveExamList(String orgId ,HashMap<String, Object> params) {
		String str="";
		HospitalDict hospitalDict = new HospitalDict();
		hospitalDict.setOrgId(orgId);
		hospitalDict = hospitalDictDao.getHospitalDictsByEntity(hospitalDict);
		String serverIp = "http://";
		if(hospitalDict!=null)
			serverIp += hospitalDict.getValue();
		RestUtils<BaseDto,HashMap> restUtils =new RestUtils<BaseDto ,HashMap>(serverIp);
		if(orgId.equals("861100292")){
			Map<String, Object> map = restUtils.findById("/api/reg/save", BaseDto.class, params, params, "");
			BaseDto dto= (BaseDto) map.get("body");
			str = (String)dto.get("strstate");
		}else{
			Map<String, Object> map = restUtils.findByList("/net/clinicApi/hisExamAppointsSave", BaseDto.class, params,params);
			List<BaseDto> list= (List<BaseDto>) map.get("body");
			if(list!=null&&list.size()>0){
				BaseDto dto = list.get(0);
				str = (String)dto.get("strstate");
			}
		}
		return str;
	}
    /**
     * 大字段转字符串
     *
     * @param src clob 解码
     * @return 字符串
     * @throws Exception 异常
     */
    public static String unescape(String src) {
        StringBuffer tmp = new StringBuffer();
        if (src == null || "".equals(src)) {
            return "";
        } else {
            tmp.ensureCapacity(src.length());
            int lastPos = 0, pos = 0;
            char ch;
            while (lastPos < src.length()) {
                pos = src.indexOf("%", lastPos);
                if (pos == lastPos) {
                    if (src.charAt(pos + 1) == 'u') {
                        ch = (char) Integer.parseInt(src
                                .substring(pos + 2, pos + 6), 16);
                        tmp.append(ch);
                        lastPos = pos + 6;
                    } else {
                        ch = (char) Integer.parseInt(src
                                .substring(pos + 1, pos + 3), 16);
                        tmp.append(ch);
                        lastPos = pos + 3;
                    }
                } else {
                    if (pos == -1) {
                        tmp.append(src.substring(lastPos));
                        lastPos = src.length();
                    } else {
                        tmp.append(src.substring(lastPos, pos));
                        lastPos = pos;
                    }
                }
            }
            return tmp.toString();
        }
    }

}
